package com.hmall.item.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmall.common.dto.PageDTO;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("item")
public class ItemController {

    @Autowired
    private IItemService itemService;

    @GetMapping("/list")//分页查询
    public PageDTO<Item> zxc(@RequestParam("page")Integer page, @RequestParam("size")Integer size){
        Page<Item> itemPage = itemService.page (new Page<> (page, size));
        return new PageDTO<> (itemPage.getTotal (),itemPage.getRecords ());//返回数据
    }

    @GetMapping//根据id查询
    public Item getBuId(@PathVariable("id") Integer id) {
        return itemService.getById (id);
    }

    @PostMapping//添加
    public void add(@RequestBody Item item){
        itemService.save(item);
    }

    @PutMapping("/status/{id}/{status}")//修改
    public void status(@PathVariable("id") Long id,@PathVariable("status") Integer status){

        Item item = itemService.getById(id);
        item.setStatus(status);
        itemService.updateById(item);
    }

    @DeleteMapping("{id}")//id删除
    public void deleteById(@PathVariable("id") Integer id){
        itemService.removeById (id);

    }

}
